package com.example.factory.api.facade.impl;

import com.example.factory.api.dto.DtoLevel1Dimension;
import com.example.factory.api.dto.DtoLevel2Dimension;
import com.example.factory.api.dto.Response;
import com.example.factory.api.dto.assembler.GradingRuleAssembler;
import com.example.factory.api.dto.form.DtoLevel1DimensionForm;
import com.example.factory.api.dto.form.DtoLevel2DimensionForm;
import com.example.factory.api.facade.GradingRuleFacade;
import com.example.factory.core.domain.Level1Dimension;
import com.example.factory.core.domain.Level2Dimension;
import com.example.factory.core.repository.Level1DimensionRepository;
import com.example.factory.core.repository.Level2DimensionRepository;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Lei Tao
 */
@Service
public class GradingRuleFacadeImpl implements GradingRuleFacade {
    private final Level1DimensionRepository level1DimensionRepository;
    private final Level2DimensionRepository level2DimensionRepository;
    private final GradingRuleAssembler gradingRuleAssembler;

    public GradingRuleFacadeImpl(Level1DimensionRepository level1DimensionRepository, Level2DimensionRepository level2DimensionRepository, GradingRuleAssembler gradingRuleAssembler) {
        this.level1DimensionRepository = level1DimensionRepository;
        this.level2DimensionRepository = level2DimensionRepository;
        this.gradingRuleAssembler = gradingRuleAssembler;
    }

    @Override
    public Response<List<DtoLevel1Dimension>> selectedLevel1Dimensions() {
        List<Level1Dimension> level1Dimensions = level1DimensionRepository.selectedLevel1Dimensions();
        List<DtoLevel1Dimension> dtoLevel1Dimensions = gradingRuleAssembler.toDtoLevel1Dimensions(level1Dimensions);
        return Response.success(dtoLevel1Dimensions);
    }

    @Override
    public Response<DtoLevel1Dimension> selectedLevel1Dimension(String level1Guid) {
        Level1Dimension level1Dimension = level1DimensionRepository.findByGuidAndArchivedFalse(level1Guid);
        DtoLevel1Dimension dtoLevel1Dimension = gradingRuleAssembler.toDtoLevel1Dimension(level1Dimension);
        return Response.success(dtoLevel1Dimension);
    }

    @Override
    public Response<Void> deletedLevel1(String level1Guid) {
        Level1Dimension level1Dimension = level1DimensionRepository.findByGuidAndArchivedFalse(level1Guid);
        level1Dimension.archived();
        level1DimensionRepository.save(level1Dimension);
        return Response.success();
    }

    @Override
    public Response<DtoLevel1Dimension> saveOrUpdateLevel1Dimension(DtoLevel1DimensionForm dtoLevel1DimensionForm) {
        Level1Dimension level1Dimension = gradingRuleAssembler.toLevel1Dimension(dtoLevel1DimensionForm);
        level1DimensionRepository.save(level1Dimension);
        DtoLevel1Dimension dtoLevel1Dimension = gradingRuleAssembler.toDtoLevel1Dimension(level1Dimension);
        return Response.success(dtoLevel1Dimension);
    }

    @Override
    public Response<List<DtoLevel2Dimension>> selectedLevel2Dimensions(String level1Guid) {
        List<Level2Dimension> level2Dimensions = level2DimensionRepository.selectAll(level1Guid);
        List<DtoLevel2Dimension> dtoLevel2Dimensions = gradingRuleAssembler.toLevel2Dimensions(level2Dimensions);
        return Response.success(dtoLevel2Dimensions);
    }

    @Override
    public Response<DtoLevel2Dimension> selectedLevel2Dimension(String level2Guid) {
        Level2Dimension level2Dimension = level2DimensionRepository.findByGuidAndArchivedFalse(level2Guid);
        DtoLevel2Dimension dtoLevel2Dimension = gradingRuleAssembler.toDtoLevel2Dimension(level2Dimension);
        return Response.success(dtoLevel2Dimension);
    }

    @Override
    public Response<Void> deletedLevel2(String level2Guid) {
        Level2Dimension level2Dimension = level2DimensionRepository.findByGuidAndArchivedFalse(level2Guid);
        level2Dimension.archived();
        level2DimensionRepository.save(level2Dimension);
        return Response.success();
    }

    @Override
    public Response<DtoLevel2Dimension> saveOrUpdateLevel2Dimension(DtoLevel2DimensionForm dtoLevel2DimensionForm) {
        Level2Dimension level2Dimension = gradingRuleAssembler.toLevel2Dimension(dtoLevel2DimensionForm);
        level2DimensionRepository.save(level2Dimension);
        DtoLevel2Dimension dtoLevel2Dimension = gradingRuleAssembler.toDtoLevel2Dimension(level2Dimension);
        return Response.success(dtoLevel2Dimension);
    }


}
